define(function(require) {

    'use strict';

    var Backbone = require('backbone'),
        Base = require('components/pop/base'),
        template = require('text!components/pop/templates/timer.html'),
        utils = require('core/utils');

    require('datetimepicker');

    return Base.extend({

        template: _.template(template),

        events: {
            'click .js-btn-cancel': 'hide',
            'click .js-btn-confirm': 'triggerCallback'
        },

        className: function() {
            return this.options.className;
        },

        initialize: function(options) {
            Base.prototype.initialize.call(this, options);
            this.data = options.data;
        },

        render: function() {
            this.$el.html(this.template({}));
            var $input = this.$('.js-time-input');

            var time = this.data.time;

            if (time > 0) {
                var date = utils.getFullTime(new Date(time));
                $input.val(date);
            }

            $input.datetimepicker({
                minDate: new Date()
            });

            $('#ui-datepicker-div').on('click', function(e) {
                e.stopPropagation();
            });
            return this;
        },

        positioning: function() {
            var view = this;
            var className = view.options.className;

            view.$el.show();
            if (className.indexOf('left') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'right center',
                    at: 'left center',
                    collision: 'none'
                });
            } else if(className.indexOf('bottom') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'center top',
                    at: 'center bottom',
                    collision: 'none'
                });
            } else if(className.indexOf('top') !== -1) {
                view.$el.position({
                    of: view.target,
                    my: 'center bottom',
                    at: 'center top',
                    collision: 'none'
                });
            } else {
                view.$el.position({
                    of: view.target,
                    my: 'left center',
                    at: 'right center',
                    collision: 'none'
                });
            }

            view.el.className = view.options.className;
        },

        setData: function(data) {
            this.data = _.extend(this.data, data);
            this.render();
        },

        reset: function(obj) {
            var callback = obj.callback,
                target = obj.target,
                trigger = obj.trigger,
                className = obj.className || 'popover top',
                data = obj.data;

            this.options.className = className;
            this.setCallback(callback);
            this.setTarget(target);
            this.setTrigger(trigger);
            this.setData(data);
            this.positioning();
            this.show();
        },

        triggerCallback: function() {
            this.callback.call(this,
                this.$('.js-time-input').datetimepicker('getDate')
            );
            this.hide();
        }
    });
});
